#$HeadURL: http://sscreen.googlecode.com/svn/tags/R2/menu.py $
#$Id: menu.py 8 2007-10-22 08:54:49Z mateusz.kijowski $

import os
import sys
import curses
import curses.wrapper

def __init__():
    pass

class menu:
    def __init__(self,menu_dict_list):
        self.restart=0
        self.items= [
                     menuItem(m['txt'], menu_dict_list.index(m), m['fun'], m['args']) 
                     for m in menu_dict_list 
                    ]
    def dostart(self):
        curses.wrapper(self._start)
    def dorestart(self):
        self.restart=1
    def _start(self,stdscr):
        curses.use_default_colors()
        vtab=10
        htab=20
        maxlen=65
        
        #set title
        stdscr.addstr(0,1,sys.argv[0],curses.A_NORMAL)
        stdscr.refresh()
        
        self.win=curses.newwin(len(self.items)+1,maxlen,vtab,htab)
        self.win.keypad(1)

        self.previous_item = self.items[0]
        self.current_item = self.items[0]
        for item in self.items:
            item.set_win(self.win)
            item.display()
        self.current_item.activate()
        while not self.restart:
            c = self.win.getch()
            if c == ord('q'):
                break
            elif c in (curses.KEY_UP, ord('k')):
                self.previous_item = self.current_item
                if self.current_item.number == 0:
                    self.current_item = self.items[len(self.items) -1 ]
                else:
                    self.current_item = self.items[self.current_item.number - 1]
                self.update()
                    
            elif c in (curses.KEY_DOWN, ord('j')):
                self.previous_item = self.current_item
                if self.current_item.number == len(self.items) -1:
                    self.current_item = self.items[0]
                else:
                    self.current_item = self.items[self.current_item.number + 1]
                self.update()                    
            elif c in (curses.KEY_ENTER, ord('l'),10):
                self.current_item.run()
                self.win.refresh()
        curses.beep()
    def update(self):
        self.previous_item.deactivate()
        self.current_item.activate()

class menuItem:
    def __init__(self,txt,number,function,args):
        self.activated=0
        self.txt=txt
        self.number=number
        self.function=function
        self.args=args
        self.win=0
    def run(self):
        apply(self.function,self.args)
    def set_win(self,win):
        self.win=win
    def activate(self):
        self.activated=1
        self.display()
    def deactivate(self):
        self.activated=0
        self.display()
    def display(self):
        self.win.addstr(self.number,0,self.txt,
                        self.activated == 1 and
                            curses.A_REVERSE or curses.A_NORMAL)
if __name__ == "__main__":
    menu_dict_list = [
              dict(txt="pierwsze", fun=getattr(os,"system"), args=("screen -t nowy")),
              dict(txt="drugie", fun=getattr(os,"system"), args=("screen -t nowy")),
              dict(txt="trzecie", fun=getattr(os,"system"), args=("screen -t nowy")),
              dict(txt="czwarte", fun=getattr(os,"system"), args=("screen -t nowy"))
              ]
    print menu_dict_list
    m = menu(menu_dict_list)